﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication15
{
    public partial class LoginForm : Form
    {
        #region "Biến cục bộ trong Form"
            string txtAgentID;
            private string _resultText;
        #endregion

        public LoginForm()
        {
            InitializeComponent();
        }

        public string SendMessage
        {
            get { return _resultText; }
            private set { _resultText = value; }
        }

        public bool CheckLogin(string u, string p)
        {
            bool result = false;
            sqlConnection1.Open();
            SqlCommand theCommand = new SqlCommand("Select * from txtUser where UserName = '" + u + "' and UserPass = '" + p + "'", sqlConnection1);
            SqlDataReader theReader = theCommand.ExecuteReader();
            if (theReader.HasRows)
            {
                theReader.Read();
                txtAgentID = theReader["UserID"].ToString();
                result = true;
                theReader.Close();
            }
            else
            {
                result = false;
            }
            sqlConnection1.Close();
            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (CheckLogin(tbUser.Text, tbPass.Text))
            {
                this.DialogResult = DialogResult.OK;
                SendMessage = txtAgentID;
            }
            else
            {
                MessageBox.Show("Kiem tra thong tin dang nhap");
            }
        }
    }
}
